<?php
require("../config.php");

if (!file_exists($cfg_arr['storage'])) {
	$ret = [
		'error' => 1,
		'msg' => '未连接到数据库'
	];
	exit(json_encode($ret));
}
function login_out(){
$_SESSION['user']="";
header('Location: /index.php');
}
if(isset($_GET['act']))
{
$act=$_GET['act'];
if($act=="login_out"){
        login_out();
        exit;
}
}

require("data/sql_ope.php");
if (isset($_POST['username']) && isset($_POST['password'])) {
	$user = $_POST['username'];
	$pass = $_POST['password'];
	$time = $_POST['time'];
	$num = time() - $time;
	if ($num < 0 || $num > 60 * 5) {
		$ret = [
			'error' => -1,
			'msg' => 'null'
		];
		exit(json_encode($ret));
	}
	/*  判断是否为数字 */
	if (!intval($user)) {
		$ret = [
			'error' => 1,
			'msg' => 'QQ账号不正确'
		];
		exit(json_encode($ret));
	}
	/* 判断QQ长度 */
	if (strlen($user) < 5 && strlen($user) > 11) {
		$ret = [
			'error' => 1,
			'msg' => 'QQ账号长度请在5~11之间'
		];
		exit(json_encode($ret));
	}
	/* 判断密码长度 */
	if (strlen($pass) < 6 && strlen($pass) > 16) {
		$ret = [
			'error' => 1,
			'msg' => '密码长度请在6~16之间'
		];
		exit(json_encode($ret));
	}
	/* 密码查询验证 */
	$npass = $tq_user->password($user);
	$pass = md5($pass);
	if ($pass !== $npass) {
		$ret = [
			'error' => 1,
			'msg' => '账号密码错误'
		];
		exit(json_encode($ret));
	} else {
		$cookies = $tq_user->login($user);
		$_SESSION['user'] = $user;
		$_SESSION['cookies'] = $cookies;
		$ret = [
			'error' => 0,
			'msg' => '登陆成功！'
		];
		exit(json_encode($ret));
	}
} else {
	//header('Location: ../err/404.html'); //跳转到某一个页面，推荐使用这种方法 
	exit();
}
